package com.game;

import java.io.*;
import java.net.*;

public class Client extends Thread
{

	// 远程的IP字符串
	String remoteIPString = null;
	// 远程的服务端口
	int port;
	// 临时套接字
	Socket tempSocket;
	// 发送文件用的输出流
	OutputStream outSocket;
	InputStream inSocket;
	// 欲发送的文件
	RandomAccessFile outFile;
	// 发送文件用的临时缓存区
	byte byteBuffer[] = new byte[1024];
	private String mfilePath;
	private int bufferSize = 1024;

	/**
	 * 构造方法仅用于选择发送文件的位置 并从外部接收远程地址和端口
	 * 
	 */
	public Client(String remoteIPString, int port)
	{
		try
		{
			this.remoteIPString = remoteIPString;
			this.port = port;

		}
		catch (Exception e)
		{
		}
	}

	/**
	 * 先决条件是服务器端先开启
	 * 
	 */
	public void run()
	{
		try
		{
			this.tempSocket = new Socket(this.remoteIPString, this.port);
			System.out.println("与服务器连接成功!");

			int amount;
			outSocket = tempSocket.getOutputStream();
			inSocket = tempSocket.getInputStream();

			writeString(outSocket, getFileInfo(mfilePath));
			sleep(500);

			while (true)
			{
				String command = readString(inSocket);
				if (!command.contains("ready=1"))
				{
					continue;
				}

				System.out.println("开始发送文件...");
				while ((amount = outFile.read(byteBuffer)) != -1)
				{
					outSocket.write(byteBuffer, 0, amount);
					System.out.println("文件发送中...");
				}
				System.out.println("Send File complete");
				outFile.close();

				tempSocket.close();
				break;
			}

		}
		catch (Exception e)
		{
			System.out.println(e.toString());
			e.printStackTrace();
		}

	}

	public void sendFile(String filePath)
	{
		try
		{
			mfilePath = filePath;
			outFile = new RandomAccessFile(filePath, "r");

			this.start();
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public String getFileInfo(String filePath) throws Exception
	{
		File tempFile = new File(filePath);
		String fileName = tempFile.getName();
		long fileSize = tempFile.length();
		long lastModifyTime = tempFile.lastModified();
		String fileInfos;
		fileInfos = "MoleQInfo";
		fileInfos += "<FileName>" + fileName + "</FileName>";
		// fileInfos += "<FilePointerStart>" + offset + "</FilePointerStart>";
		// fileInfos += "<FilePointerEnd>" + endset + "</FilePointerEnd>";
		fileInfos += "<FileLength>" + fileSize + "</FileLength>";
		// fileInfos += "<LastModifyTime>" + lastModifyTime +
		// "</LastModifyTime>";
		// fileInfos += "<FileMD5>"+ getHash(filePath)+"</FileMD5>";
		return fileInfos;
	}

	public void writeString(OutputStream os, String context) throws IOException
	{
		OutputStreamWriter osw = new OutputStreamWriter(os);
		BufferedWriter writer = new BufferedWriter(osw);

		writer.write(context);
		writer.flush();
	}

	public String readString(InputStream is) throws IOException
	{
		String result = "";

		byte[] buffer = new byte[bufferSize];

		String recBuffer = "";
		int readCount = 0;
		while ((readCount = is.read(buffer, 0, bufferSize)) > 0)
		{

			recBuffer += new String(buffer, 0, readCount);
			if (readCount < bufferSize)
			{
				System.out.println("receive end ->" + recBuffer);
				result = recBuffer;
				break;
			}
		}
		return result;
	}

	protected void sleep(int time)
	{
		try
		{
			Thread.sleep(time);
		}
		catch (InterruptedException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args)
	{
		new Client("127.0.0.1", 6677).start();
	}

}
